package com.miyake.client.community;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.miyake.client.community.CommunityDetail.CommunityDetailHandler;
import com.miyake.client.community.PaperList.PaperListHandler;
import com.miyake.client.tools.ClickableLabel;
import com.miyake.client.tools.ImageList;
import com.miyake.client.tools.ImageListElement;
import com.miyake.client.tools.ImageList.CommunityListHandler;

public class CommunityMain extends Composite {

	public CommunityMain() {
		
		final VerticalPanel verticalPanel = new VerticalPanel();
		initWidget(verticalPanel);
		verticalPanel.setWidth("100%");
		showCommunityList(verticalPanel);
	}

	private void showCommunityList(final VerticalPanel verticalPanel) {
		verticalPanel.clear();
		ImageList privateCommunity = new PrivateCommunityList() {

			@Override
			protected boolean canCreate() {
				return true;
			}

			@Override
			protected boolean canFind() {
				return false;
			}
			
		};
		privateCommunity.addCommunityListHandler(new CommunityListHandler() {
			@Override
			public void onSelect(ImageListElement community) {
				showCommunityDetail(community, verticalPanel);
			}

			@Override
			public void onCreate(String name) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onDelete(String name) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFind() {
				// TODO Auto-generated method stub
				
			}
			
		});
		verticalPanel.add(privateCommunity);
		ImageList publicCommunity = new PublicCommunityList() {

			@Override
			protected boolean canCreate() {
				return true;
			}

			@Override
			protected boolean canFind() {
				return true;
			}
			
		};
		publicCommunity.addCommunityListHandler(new CommunityListHandler() {

			@Override
			public void onSelect(ImageListElement community) {
				showCommunityDetail(community, verticalPanel);
			}

			@Override
			public void onCreate(String name) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onDelete(String name) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onFind() {
				// TODO Auto-generated method stub
				
			}
			
		});
		verticalPanel.add(publicCommunity);
	}

	private void showCommunityDetail(ImageListElement prop, final VerticalPanel verticalPanel) {
		verticalPanel.clear();
		CommunityDetail paperList = new CommunityDetail(prop);
		paperList.addPaperListHandler(new PaperListHandler() {
			@Override
			public void onPaperSelect(String key) {
				showPaper(key, verticalPanel);
			}
		});
		verticalPanel.add(paperList);
		paperList.addCommunityDetailHandler(new CommunityDetailHandler() {
			@Override
			public void onBack() {
				showCommunityList(verticalPanel);
			}
		});
	}

	protected void showPaper(String key, final VerticalPanel verticalPanel) {
		verticalPanel.clear();
		Label back = new ClickableLabel("Back to community list");
		back.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				showCommunityList(verticalPanel);
			}
		});
		verticalPanel.add(back);
		new ShowPaper(key, verticalPanel);
	}

}
